/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package neuronalenetzeelearning.view.controlpanel;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import neuronalenetzeelearning.model.Graph;
import neuronalenetzeelearning.view.help.HelpPanel;
import neuronalenetzeelearning.view.help.HelpUtil;
import neuronalenetzeelearning.view.help.HelpWindowFactory;
/**
 *
 * @author Henning Ainödhofer
 * Diese Klasse wurde im Rahmen meiner Staatsexamensarbeit im Fach Biologie erstellt.
 */
public class PropertyGraphPanel extends GraphPanelProperty{
    DefaultTableModel tNodeModel;
    DefaultTableModel tEgdeModel;
    JPanel NodeTablePanel;
    JPanel EdgeTablePanel;
    JPanel thresholdProperty;
    JTable NodeTable;
    JTable EdgeTable;

    public PropertyGraphPanel() {
        super();
        this.setBackground(background);
        this.setLayout(grid);
        this.setBorder(javax.swing.BorderFactory.createTitledBorder("Einstellungen wählen"));

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridheight = 1;
        constraints.insets = new Insets(1, 1, 1, 1);

        tNodeModel = new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column) {
                    if (column == 0) {
                            return false;
                    } else
                            return true;
            }
        };
        tNodeModel.setColumnCount(2);
        tNodeModel.setRowCount(graph.getLayer2Nodes().size());
        for(int i = 0; i < graph.getLayer2Nodes().size(); i++){
            tNodeModel.setValueAt(i, i, 0);
        }

        tEgdeModel = new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int row, int column) {
                    if (column == 0) {
                            return false;
                    } else
                            return true;
            }
        };
        tEgdeModel.setColumnCount(2);
        tEgdeModel.setRowCount(graph.getEdges().size());
        for(int i = 0; i < graph.getEdges().size(); i++){
            tEgdeModel.setValueAt(i, i, 0);
        }

        NodeTablePanel = new JPanel();
        EdgeTablePanel = new JPanel();

        NodeTable = new JTable();
        NodeTable.setModel(tNodeModel);

        NodeTablePanel.add(NodeTable, constraints);
        NodeTablePanel.setBackground(background);
        NodeTablePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Schwellenwerte"));
        constraints.gridx = 1;
        constraints.gridy = 1;

        this.add(NodeTablePanel, constraints);

        EdgeTable = new JTable();
        EdgeTable.setModel(tEgdeModel);
        EdgeTablePanel.add(EdgeTable, constraints);
        EdgeTablePanel.setBackground(background);
        EdgeTablePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Kantengewichte"));

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.gridx = 1;
        constraints.gridy = 2;

        this.add(EdgeTablePanel, constraints);

        thresholdProperty = new JPanel();
        thresholdProperty.setBorder(javax.swing.BorderFactory.createTitledBorder("Werte automatisch generieren"));
        thresholdProperty.setBackground(background);

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        automaticThreshold = new JButton("automatische Schwellenwerte");
        automaticEgdeWeight = new JButton("automatische Kantengewichte");

        constraints.gridx = 1;
        constraints.gridy = 1;
        thresholdProperty.add(automaticThreshold, constraints);
        
        constraints.gridx = 1;
        constraints.gridy = 2;
        thresholdProperty.add(automaticEgdeWeight, constraints);

        constraints.gridx = 1;
        constraints.gridy = 3;

        this.add(thresholdProperty, constraints);

        constraints.gridwidth = GridBagConstraints.RELATIVE;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.gridx = 1;
        constraints.gridy = 4;

        navigation.add(previous, constraints);
        navigation.add(getHelpButton(), constraints);
        navigation.add(next, constraints);

        this.add(navigation, constraints);
    }

    public DefaultTableModel getDefaultTableModel(){
        return this.tNodeModel; 
    }
    public DefaultTableModel getDefaultEdgeTableModel(){
        return this.tEgdeModel;
    }
    @Override
    public void setGraph(Graph graph){
        this.graph = graph;
    }

    @Override
    public void PanelUpdate(){
        tNodeModel.setRowCount(graph.getLayer2Nodes().size());
        for(int i = 0; i < graph.getLayer2Nodes().size(); i++){
            tNodeModel.setValueAt(this.graph.getLayer2Nodes().get(i).getName(), i, 0);

        }/*
          * Beschriftung soll "K-Start zu K-Ziel" sein
          *
          */
        tEgdeModel.setRowCount(graph.getEdges().size());
        for(int i = 0; i < graph.getEdges().size(); i++){
            tEgdeModel.setValueAt(graph.getEdges().get(i).getFrom().getName()+" zu "+graph.getEdges().get(i).getTo().getName(), i, 0);
        }
        this.updateUI();
        this.repaint();
    }

    public static void main(String[] args) {
        JFrame test = new JFrame();
        test.setPreferredSize(new Dimension(800, 800));
        Graph graph = new Graph();
        test.add(new PropertyGraphPanel());
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.pack();
        test.setVisible(true);
    }

//            @Override
//    public ActionListener getActionListener() {
//        return new ActionListener() {
//
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                System.out.println("Help from Learning Rule Chooser Panel");
//            }
//        };
//
//    }

            @Override
    public ActionListener getActionListener() {
        return new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
//                HelpWindowFactory.createHelpWindow(new HelpPanel(HelpUtil.readFileToString("src/neuronalenetzeelearning/view/help/resources/propertyGraphPanelHelp")));
                HelpWindowFactory.createHelpWindow(new HelpPanel("src/neuronalenetzeelearning/view/help/resources/schwellenw-Gewichtsw.png"));
            }
        };

    }
}
